home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / autoinit.c < prev    next >
C/C++ Source or Header  |  1994-04-04  |  5KB  |  170 lines

  1. RCS_ID_C="$Id: autoinit.c,v 3.8 1994/04/04 01:25:55 jraja Exp $";
  2. /*
  3.  * autoinit.c --- SAS C auto initialization functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  12.  * Last modified: Sat Apr  2 14:25:20 1994 jraja
  13.  *
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/libraries.h>
  18.  
  19. #include <intuition/intuition.h>
  20.  
  21. #include <proto/socket.h>
  22. #include <proto/exec.h>
  23. #include <proto/intuition.h>
  24.  
  25. #include <stdlib.h>
  26.  
  27. #include <errno.h>
  28.  
  29. #include <amitcp/socketbasetags.h>
  30.  
  31. struct Library *SocketBase = NULL;
  32.  
  33. static const char SOCKETNAME[] = "bsdsocket.library";
  34.  
  35. #define SOCKETVERSION 3        /* minimum bsdsocket version to use */
  36.  
  37. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  38.  
  39. /*
  40.  * Globa| h_errno
  41.  */
  42. #if sizeof(int) != sizeof(long)
  43. #error short ints not supported!
  44. #endif
  45. int h_errno = 0;
  46.  
  47. /****** net.lib/autoinit *********************************************
  48.  
  49.     NAME
  50.         autoinit - SAS C Autoinitialization Functions
  51.  
  52.     SYNOPSIS
  53.         LONG _STI_200_openSockets(void)
  54.  
  55.         void _STD_200_closeSockets(void)
  56.  
  57.     FUNCTION
  58.         These functions open and close the bsdsocket.library at the startup
  59.         and exit of the program, respectively.  For a program to use these
  60.         functions, it must be linked with netlib:net.lib (or some variant).
  61.         These functions are linked in only if the program references the
  62.         global symbol "SocketBase".
  63.  
  64.         If the library can be opened, the _STI_200_openSockets() calls
  65.         bsdsocket.library function SocketBaseTags() to tell the library the
  66.         address and the size of the errno variable of the calling program,
  67.         the program name (to be used in syslog() messages) and the address
  68.         of the h_error variable (in which the name resolver errors are
  69.         returned).
  70.  
  71.     NOTES
  72.         _STI_200_openSockets() also checks that the system version is at
  73.         least 37. It also puts up a requester if the bsdsocket.library is
  74.         not found or is of wrong version.
  75.  
  76.         The autoinitialization and autotermination functions are features
  77.         specific to the SAS C6.  However, these functions can be used with
  78.         other (ANSI) C compilers, too. Example follows:
  79.  
  80.         \* at start of main() *\
  81.  
  82.         atexit(_STD_200_closeSockets);
  83.         if (_STD_200_openSockets() != 0)
  84.        exit(20);
  85.  
  86.     BUGS
  87.         The same autoinitialization won't work for both SAS C 6.3 and SAS C
  88.         6.50 or latter.  Only way to terminate an initialization function is
  89.         by exit() call with SAS C 6.3 binary.  If an autoinitialization
  90.         function is terminated by exit() call with SAS C 6.50 binary, the
  91.         autotermination functions won't be called.  Due this braindamage
  92.         these compilers require separate net.lib libraries.
  93.  
  94.     SEE ALSO
  95.         bsdsocket.library/SocketBaseTags(),
  96.         SAS/C 6 User's Guide p. 145 for details of autoinitialization and
  97.         autotermination functions.
  98.  
  99. *****************************************************************************
  100. */
  101.  
  102. /* SAS C 6.50 kludge */
  103. #if __VERSION__ > 6 || __REVISION__ >= 50
  104. #define exit(x) return(x)
  105. #endif
  106.  
  107. /*
  108.  * Using __stdargs prevents creation of register arguments entry point.
  109.  * If both stack args and reg. args entry points are created, this
  110.  * function is called _twice_, which is not wanted.
  111.  *
  112.  * The number 200 in the function names is the priority assigned to
  113.  * shared library autoinitialization functions by SAS/C 6.50.
  114.  */
  115. LONG __stdargs
  116. _STI_200_openSockets(void)
  117. {
  118.   struct Library *IntuitionBase;
  119.   STRPTR errorStr;
  120.  
  121.   /*
  122.    * Check OS version
  123.    */
  124.   if ((*(struct Library **)4)->lib_Version < 37)
  125.     exit(20);
  126.  
  127.   /*
  128.    * Open bsdsocket.library
  129.    */
  130.   if ((SocketBase = OpenLibrary((STRPTR)SOCKETNAME, SOCKETVERSION)) != NULL) {
  131.     /*
  132.      * Succesfull. Now tell bsdsocket.library the address of our errno
  133.      */
  134.     if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
  135.                SBTM_SETVAL(SBTC_HERRNOLONGPTR), &h_errno,
  136.                SBTM_SETVAL(SBTC_LOGTAGPTR), _ProgramName,
  137.                TAG_END))
  138.       exit(30);
  139.     return 0;
  140.   }
  141.   else
  142.     errorStr = "AmiTCP/IP version 3 or later must be started first.";
  143.  
  144.   IntuitionBase = OpenLibrary("intuition.library", 36);
  145.  
  146.   if (IntuitionBase != NULL) {
  147.     struct EasyStruct libraryES;
  148.  
  149.     libraryES.es_StructSize = sizeof(libraryES);
  150.     libraryES.es_Flags = 0;
  151.     libraryES.es_Title = _ProgramName;
  152.     libraryES.es_TextFormat = errorStr;
  153.     libraryES.es_GadgetFormat = "Exit %s";
  154.  
  155.     EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
  156.  
  157.     CloseLibrary(IntuitionBase);
  158.   }
  159.   exit(20);
  160. }
  161.  
  162. void __stdargs
  163. _STD_200_closeSockets(void)
  164. {
  165.   if (SocketBase) {
  166.     CloseLibrary(SocketBase);
  167.     SocketBase = NULL;
  168.   }
  169. }
  170.